﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;

namespace _2011DBProject
{
    public partial class Shop : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnBuy.Attributes.Add("onclick", "return confirm('선택하신 아이템을 구입하시겠습니까?');");
        }

        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //아이템 구매
            lblEnd.Visible = false;
            btnBuy.Visible = true;
        }
        
        

        private decimal getNextInven_Item_Uniq_No(decimal itemno)
        {
            decimal r = 1;
            try
            {
                System.Data.OracleClient.OracleConnection cnn = new System.Data.OracleClient.OracleConnection(Settings.strConnectionString);
                System.Data.OracleClient.OracleCommand command = new System.Data.OracleClient.OracleCommand("select count(*) from MY_ITEM where CHARACTER_NAME = '" + Settings.strCharacterName + "' and SERVER_NO = " + Settings.decServer + " and ITEM_NO = '" + itemno + "'", cnn);
                cnn.Open();
                r = Convert.ToDecimal(command.ExecuteScalar());
                cnn.Close();
                return r;
            }
            catch (Exception)
            {
                throw;
            }
        }
        protected void btnGoLogout_onclick(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("Default.aspx");
        }
        protected void btnGoServer_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Server.aspx");
        }
        protected void btnGoCharacter_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Character.aspx");
        }
        protected void btnGoChannel_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Channel.aspx");
        }
        protected void btnGoMain_onclick(object sender, EventArgs e)
        {
            Response.Redirect("Gameroom.aspx");
        }
        protected void btnBuy_Click1(object sender, EventArgs e)
        {
            //아이템 구매
            try
            {
                DataSet1.CHARACTERDataTable character = new DataSet1.CHARACTERDataTable();
                DataSet1TableAdapters.CHARACTERTableAdapter taCharacter = new DataSet1TableAdapters.CHARACTERTableAdapter();
                DataSet1.V_SHOP_ITEM_DETAILDataTable VShopItem = new DataSet1.V_SHOP_ITEM_DETAILDataTable();
                DataSet1TableAdapters.V_SHOP_ITEM_DETAILTableAdapter taVShopItem = new DataSet1TableAdapters.V_SHOP_ITEM_DETAILTableAdapter();
                DataSet1.MY_ITEMDataTable myItem = new DataSet1.MY_ITEMDataTable();
                DataSet1TableAdapters.MY_ITEMTableAdapter taMyItem = new DataSet1TableAdapters.MY_ITEMTableAdapter();
                taCharacter.Connection.ConnectionString = Settings.strConnectionString;
                taVShopItem.Connection.ConnectionString = Settings.strConnectionString;
                taMyItem.Connection.ConnectionString = Settings.strConnectionString;

                if (taCharacter.FillByServerNoCharacterName(character, Settings.decServer, Settings.strCharacterName) > 0)
                {
                    if (taVShopItem.FillByShopNoItemNo(VShopItem, (decimal)GridView2.SelectedDataKey[0], (decimal)GridView2.SelectedDataKey[1]) > 0)
                    {
                        decimal price = 0;
                        if (character[0].IsNG_AMOUNTNull()) character[0].NG_AMOUNT = 0;
                        if (!VShopItem[0].IsPRICENull()) price = VShopItem[0].PRICE;

                        if (character[0].NG_AMOUNT < price)
                            MessageBox.Show("엔지가 모자릅니다.");
                        else
                        {
                            character[0].NG_AMOUNT -= price;
                            //인벤토리에 저장
                            DataSet1.MY_ITEMRow row = myItem.NewMY_ITEMRow();
                            row.CADDIE_EQUIP = "N";
                            row.CHARACTER_EQUIP = "N";
                            row.CHARACTER_NAME = Settings.strCharacterName;
                            row.DURABILITY = 0;
                            row.ITEM_NO = VShopItem[0].ITEM_NO;
                            row.LOCKERROOM_KEEP = "N";
                            if (VShopItem[0].IsPOWER_CORRECTIONNull())
                                row.POWER_CORRECTION = 0;
                            else
                                row.POWER_CORRECTION = VShopItem[0].POWER_CORRECTION;
                            row.QTY = 1;
                            row.SERVER_NO = Settings.decServer;
                            if (VShopItem[0].IsSTRENGTH_CORRECTIONNull())
                                row.STRENGTH_CORRECTION = 0;
                            else
                                row.STRENGTH_CORRECTION = VShopItem[0].STRENGTH_CORRECTION;
                            if (VShopItem[0].IsTECHNIQUE_CORRECTIONNull())
                                row.TECHNIQUE_CORRECTION = 0;
                            else
                                row.TECHNIQUE_CORRECTION = VShopItem[0].TECHNIQUE_CORRECTION;

                            row.INVEN_ITEM_UNIQ_NO = getNextInven_Item_Uniq_No(row.ITEM_NO);
                            if (VShopItem[0].IsIMPACT_CORRECTIONNull())
                                row.IMPACT_CORRECTION = 0;
                            else
                                row.IMPACT_CORRECTION = VShopItem[0].IMPACT_CORRECTION;
                            myItem.AddMY_ITEMRow(row);
                            taMyItem.Update(myItem);
                            taCharacter.Update(character);
                            gvMyItem.DataBind();
                            lblEnd.Visible = true;
                            //Response.Redirect("Shop.aspx");
                        }
                    }
                }

            }
            catch (Exception)
            {
                throw;
            }
        }

        //protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        //{
        //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //    {
        //        DataRowView dr = e.Item.DataItem as DataRowView;
        //        Button btnBuy = e.Item.FindControl("btnBuy") as Button;
        //        btnBuy.CommandArgument = dr["ITEM_NOLabel"].ToString();
        //    }
        //}

        //protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        //{
        //    if (e.CommandName == "itemSelect")
        //    {
        //        //Session.Add("SERVER", Convert.ToInt32(e.CommandArgument));
        //        //Settings.decServer = Convert.ToInt32(e.CommandArgument);
        //        //Response.Redirect("Character.aspx");
        //    }
        //}
    
    }
}
